package cn.org.timespace.easyframe.util.jasper;

import java.io.IOException;
import java.lang.reflect.Method;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;

/**
 * <p>
 * 报表展现和导出的Servlet
 * </p>
 * 
 * @version 1.0, 11/01/18
 * @since 1.0
 * @author 
 */

public class EfJasperReportServlet extends HttpServlet {
	
	private static final String REPORT_CLASS_PATH = "reportClassPath";
	private static final String TYPE = "type";
	

	private static final long serialVersionUID = 1615786124349294518L;

	public void init(ServletConfig config) throws ServletException {
		super.init(config);
	}

	public void service(ServletRequest request, ServletResponse response)
			throws ServletException, IOException {
		String reportClassPath = (String) request.getParameter(REPORT_CLASS_PATH);
		String type = (String) request.getParameter(TYPE); // view or export
		
		// no reportName
		if (reportClassPath == null || reportClassPath.equals("")) {
			request.setAttribute("errorMessage","no parameter of 'reportName' !");
			return;
		}
		// no type
		if (type == null || type.equals("")) {
			request.setAttribute("errorMessage", "no parameter of 'type' !");
			return;
		}
		
		try {
			Class reportClass = Class.forName(reportClassPath);

			Method viewReportMethod = reportClass.getMethod(type, new Class[] {ServletRequest.class, ServletResponse.class });

			viewReportMethod.invoke(reportClass.newInstance(), new Object[] {request, response });
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void destroy() {
		super.destroy();
	}

}
